﻿using System;
using System.Web;
using System.Web.Security;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.ComponentModel;

namespace DalatCooking.Core.Helper
{   
   public class MyEncode
    {
       public static string Encode(string clearText)
       {
           clearText = FormsAuthentication.HashPasswordForStoringInConfigFile(clearText, "MD5");
           string key = "azsxc65dc26006ca53138c3f2906c4758eaf1902";
           key = FormsAuthentication.HashPasswordForStoringInConfigFile(key, "SHA1");

           string lastText = clearText + key;
           lastText = FormsAuthentication.HashPasswordForStoringInConfigFile(lastText, "SHA1");
           return lastText;
       }
       public static string Encrypt(string value)
       {
           if (string.IsNullOrEmpty(value))
               return string.Empty;
           var md5 = new MD5CryptoServiceProvider();
           byte[] valueArray = Encoding.ASCII.GetBytes(value);
           valueArray = md5.ComputeHash(valueArray);
           var sb = new StringBuilder();
           for (int i = 0; i < valueArray.Length; i++)
               sb.Append(valueArray[i].ToString("x2").ToLower());
           return sb.ToString();
       }
       public static string EncodeMD5(string str)
       {
           Byte[] original;
           Byte[] encode;

           MD5 md5 = new MD5CryptoServiceProvider();

           original = ASCIIEncoding.Default.GetBytes(str);
           encode = md5.ComputeHash(original);         
           return BitConverter.ToString(encode);
       }      
       public static string EncodeSHA1(string str)
       {
           string ret = String.Empty;
           Byte[] original;
           Byte[] encode;
           SHA1  sha1 = new SHA1CryptoServiceProvider();
           original = Encoding.Default.GetBytes(str);
           encode = sha1.ComputeHash(original);
           for (int i = 0; i < encode.Length; i++)
           {
               ret += encode[i].ToString("x3").ToUpper();
           }
           //return BitConverter.ToString(encode);
           return ret;
       }
       public static string EncodeSHA256(string str)
       {           
           Byte[] original;
           Byte[] encode;
           SHA256 sha256 = new SHA256CryptoServiceProvider();
           original = ASCIIEncoding.Default.GetBytes(str);
           encode = sha256.ComputeHash(original);
           return BitConverter.ToString(encode);
       }
       public static string EncodeSHA384(string str)
       {
           Byte[] original;
           Byte[] encode;
           SHA384 sha384 = new SHA384CryptoServiceProvider();
           original = ASCIIEncoding.Default.GetBytes(str);
           encode = sha384.ComputeHash(original);
           return BitConverter.ToString(encode);
       }   
       public static string EncodeSHA512(string str)
       {           
           Byte[] original;
           Byte[] encode;
           SHA512 sha512 = new SHA512CryptoServiceProvider();
           original = ASCIIEncoding.Default.GetBytes(str);
           encode = sha512.ComputeHash(original);
           return BitConverter.ToString(encode);
       }      
    }
}
